package cn.echogo.framework.core;

import java.lang.ref.WeakReference;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ViewHelperImp implements ViewHelper {

	private WeakReference<Activity> mActivityRef;

	public ViewHelperImp(Activity activity) {
		mActivityRef = new WeakReference<Activity>(activity);
	}

	@Override
	public void showProgressBar(int viewGroupId, int id) {
		Activity activity = mActivityRef.get();
		if (activity != null) {
			if (activity instanceof FragmentActivity) {
				ProgressBarFragment fragment = new ProgressBarFragment();
				FragmentActivity fActivity = (FragmentActivity) activity;
				// TODO
			} else {
				ViewGroup viewGroup = (ViewGroup) activity.findViewById(viewGroupId);
				ProgressBar progressBar = (ProgressBar) viewGroup.findViewById(id);
				if (progressBar == null) {
					progressBar = new ProgressBar(activity);
					progressBar.setId(id);
					addChildViewToViewGroup(viewGroup, progressBar);
				}
				progressBar.setVisibility(View.VISIBLE);
			}
		}
	}

	@Override
	public void hideProgressBar(int viewGroupId, int id) {
		Activity activity = mActivityRef.get();
		if (activity != null) {
			if (activity instanceof FragmentActivity) {
				// TODO
			} else {
				ViewGroup viewGroup = (ViewGroup) activity.findViewById(viewGroupId);
				ProgressBar progressBar = (ProgressBar) viewGroup.findViewById(id);
				if (progressBar != null) {
					progressBar.setVisibility(View.GONE);
				}
			}
		}
	}

	private static class ProgressBarFragment extends Fragment {
		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
			ProgressBar progressBar = new ProgressBar(getActivity());
			progressBar.setVisibility(View.VISIBLE);
			return progressBar;
		}
	}

	@Override
	public void showTipText(int viewGroupId, int id, String text) {
		Activity activity = mActivityRef.get();
		if (activity != null) {
			if (activity instanceof FragmentActivity) {
				// TODO
			} else {
				ViewGroup viewGroup = (ViewGroup) activity.findViewById(viewGroupId);
				TextView textView = (TextView) viewGroup.findViewById(id);
				if (textView == null) {
					textView = new TextView(activity);
					addChildViewToViewGroup(viewGroup, textView);
				}
				textView.setVisibility(View.VISIBLE);
				textView.setText(text);
			}
		}
	}

	@Override
	public void hideTipText(int viewGroupId, int id) {
		Activity activity = mActivityRef.get();
		if (activity != null) {
			if (activity instanceof FragmentActivity) {
				// TODO
			} else {
				ViewGroup viewGroup = (ViewGroup) activity.findViewById(viewGroupId);
				TextView textView = (TextView) viewGroup.findViewById(id);
				if (textView != null) {
					textView.setVisibility(View.GONE);
				}
			}
		}
	}

	/**
	 * 添加子视图到组视图里，居中显示
	 * 
	 * @param viewGroup
	 * @param tChildView
	 */
	private <T> void addChildViewToViewGroup(ViewGroup viewGroup, T tChildView) {
		View childView = (View) tChildView;
		if (viewGroup instanceof FrameLayout) {
			FrameLayout.LayoutParams prm = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
			prm.gravity = Gravity.CENTER;
			childView.setLayoutParams(prm);
		} else if (viewGroup instanceof LinearLayout) {
			LinearLayout.LayoutParams prm = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
			prm.gravity = Gravity.CENTER;
			childView.setLayoutParams(prm);
		} else if (viewGroup instanceof RelativeLayout) {
			RelativeLayout.LayoutParams prm = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
			prm.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
			childView.setLayoutParams(prm);
		}
		viewGroup.addView(childView);
	}

}
